package presstrack
{
	import flash.net.Responder;
	import flash.utils.Dictionary;
	
	import mx.utils.ObjectUtil;
	
	import org.presstrack.service.*;
	import org.presstrack.vos.TrackStateVO;
	
	[RemoteClass(alias="presstrack.TaskClass")]
	
	[Bindable]
	public class TaskClass
	{
		public var idtask:int;
		
  		public var label:String;
  		public var value:String;
  		public var section:int;
  		
  		public var execute:String;
  		public var cost:String;
  		
  		public var state:TrackStateVO;
  		public var state_note:String;
  		
  		public var start_day:String;
  		public var start_time:String;
  		public var end_day:String;
  		public var end_time:String; 		
  		
  		public var order:int;
  		
  		public var product:String;
  		public var ob_value:Dictionary;
				
		public function TaskClass(task:Object = null) 
		{
			if(task != null) setFrom(task);
			else {
				cost 	= "0.00";
				state 	= new TrackStateVO("1");
				value 	= '';
				product = '';
				execute = CrudService.company;
			}			
		}
		
		public function setFrom(ob:Object):void{
			if (ob.hasOwnProperty("idtask"))
				idtask = ob.idtask.toString();
				
			if (ob.hasOwnProperty("label"))
				label = ob.label.toString();
				
			if (ob.hasOwnProperty("value")) {
				value = ob.value.toString();
				
				ob_value = new Dictionary();
				var temp1:Array = value.split("\r");
				for each (var str:String in temp1)
				{
					var temp2:Array = str.split(" : ");
					if (temp2.length > 1) 
					{
						var p:String = temp2[0].toString();
						var v:String = temp2[1].toString();
						ob_value[p] = v;
					}							
				}
			}
				
			if (ob.hasOwnProperty("execute"))
				execute = ob.execute.toString();
				
			if (ob.hasOwnProperty("cost"))
				cost = Number(ob.cost).toFixed(2);
				
			if (ob.hasOwnProperty("section"))
				section = ob.section;
				
			if (ob.hasOwnProperty("state"))
				state = new TrackStateVO(ob.state.toString());
				
			if (ob.hasOwnProperty("state_note"))
				state_note = ob.state_note;
				
			if (ob.hasOwnProperty("start_day"))
				start_day = ob.start_day;
				
			if (ob.hasOwnProperty("start_time"))
				start_time = ob.start_time;
				
			if (ob.hasOwnProperty("order"))
				order = ob.order;
				
			if (ob.hasOwnProperty("product"))
				product = ob.product.toString();
		}
		
		public function setProducts(ob:Object, product_label:String) : void {
			product		= product_label;
		}
		
		public function compare(ob:Object) : Boolean {
			if (order != ob.order) return false;
			if (section != ob.section) return false;
			if (execute != ob.execute) return false;
			if (label != ob.label) return false;
			if (value != ob.value) return false;
			return true;
		} 
		
		public function setValue(_value:String, resFunc:Function = null, faultFunc:Function = null):void {
			updateParam("value", _value, resFunc, faultFunc);
		}
		
		public function setCost(_value:String, resFunc:Function = null, faultFunc:Function = null):void {
			updateParam("cost", _value, resFunc, faultFunc);
		}
		
		public function setExecute(_value:String, resFunc:Function = null, faultFunc:Function = null):void {
			if (_value == "") _value = CrudService.company;
			updateParam("execute", _value, resFunc, faultFunc);
		}
		
		public function setState(_value:String, resFunc:Function = null, faultFunc:Function = null):void {
			updateParam("state", _value, resFunc, faultFunc);
		}
		
		public function setStateNote(_value:String, resFunc:Function = null, faultFunc:Function = null):void {
			updateParam("state_note", _value, resFunc, faultFunc);
		}
		// private
		
		private function updateParam(param		:String, 
									 _value		:String, 
									 resFunc	:Function	= null, 
									 faultFunc	:Function	= null):void 
		{
			CrudService.crud.srv.call("PressTrack.UpdateTask", new Responder(onResult, onFault), idtask, param, _value);
			
			function onResult(res:String):void 
			{
				if (res == 'successful') 
				{
					if (param == "value")
					{
						value = _value;
						ob_value = new Dictionary();
						var temp1:Array = value.split("\r");
						for each (var str:String in temp1)
						{
							var temp2:Array = str.split(" : ");
							if (temp2.length > 1) {
								var p:String = temp2[0].toString();
								var v:String = temp2[1].toString();
								ob_value[p] = v;
							}
						}
					}
					else if (param == "cost")
						cost = Number(_value).toFixed(2);
					else if(param == "state")
						state = new TrackStateVO(_value);
					else if(param == "execute")
						execute = _value;
					
					if (resFunc != null)
					{
						resFunc();
					}
				}
				else	
					faultFunc(res);			
			}
			
			function onFault(e:Object):void 
			{
				faultFunc(ObjectUtil.toString(e));
			}
		}	
	}
}